package com.wst.system.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wst.system.config.exception.GlobalExcetion;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wst.system.server.entity.SysOpenToken;
import com.wst.system.server.mapper.SysOpenTokenMapper;
import com.wst.system.server.service.SysOpenTokenService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.BeanUtils;
import com.wst.business.entity.vo.SysOpenTokenVo;
import java.io.Serializable;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author X.I.O
 * @since 2025-02-10
 */
@Service
@Transactional
public class SysOpenTokenServiceImpl extends ServiceImpl<SysOpenTokenMapper, SysOpenToken> implements SysOpenTokenService {


    @Override
    public SysOpenToken getById(Serializable id){
        return super.getById(id);
    }


    @Override
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }


    @Override
    public boolean updateById(SysOpenToken sysopentoken) {
        return super.updateById(sysopentoken);
    }


    public SysOpenToken saveSysOpenToken(SysOpenToken sysopentoken) throws GlobalExcetion {
        super.save(sysopentoken);
        return sysopentoken;
    }


    public SysOpenToken updateSysOpenTokenById(SysOpenToken sysopentoken) throws GlobalExcetion{
        super.updateById(sysopentoken);
        return sysopentoken;
    }


    public SysOpenToken saveOrUpdateSysOpenToken(SysOpenTokenVo sysopentokenVo) throws GlobalExcetion{
        SysOpenToken sysopentoken=new SysOpenToken();
        BeanUtils.copyProperties(sysopentokenVo,sysopentoken);
        if(sysopentokenVo.getId()!=null){
            SysOpenToken sysopentokenEntity=super.baseMapper.selectById(sysopentokenVo.getId());
            if(sysopentokenEntity==null){
                throw new GlobalExcetion(1,"数据不存在");
            }
            super.baseMapper.updateById(sysopentoken);
        }else{
            super.baseMapper.insert(sysopentoken);
        }
        return sysopentoken;
    }

    public IPage<SysOpenToken> page(Integer page,Integer pageSize) throws GlobalExcetion{
        LambdaQueryWrapper<SysOpenToken> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        IPage<SysOpenToken> iPage=super.baseMapper.selectPage(new Page(page,pageSize),lambdaQueryWrapper);
        return iPage;
    }

    @Override
    public SysOpenToken getSysOpenTokenByToken(String token) throws GlobalExcetion {
        LambdaQueryWrapper<SysOpenToken> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysOpenToken::getApptoken,token);
        return super.baseMapper.selectOne(lambdaQueryWrapper);
    }
}